/* ******* */
/* Objects */
/* ******* */

function user(){
	this.id;
	this.online;
	this.tech;
	this.story;
	this.raceInfo;
	this.pics;
	this.race;
	this.login;
	this.currentPage;
	this.resources;
	this.troopList;
	this.buildingList;
}

function unit(){
	this.name;
	this.price;
	this.quantity;
}

function building(){}
building.prototype = new unit();

function troop(){}
troop.prototype = new unit();

function Resources(){
	this.wood;
	this.stone;
	this.metal;
	this.gold;
	this.time;
	this.points;
}

function stat(){
	this.life;
	this.attack;
	this.defense;
	this.hability;
	this.speed;
}

/* ************** */
/* Initialisation */
/* ************** */

/* troops */
var footman = new troop();
footman.name = "footman";
footman.stat = new stat();
footman.stat.life = 10;
footman.stat.attack = 3;
footman.stat.defense = 2;
footman.stat.hability = 3;
footman.stat.speed = 2;
footman.price = new Resources();
footman.price.wood = 0;
footman.price.stone = 0;
footman.price.metal = 50;
footman.price.gold = 50;
footman.price.time = 3;
footman.price.points = 32;
footman.quantity = 0;

var archer = new troop();
archer.name = "archer";
archer.stat = new stat();
archer.stat.life = 8;
archer.stat.attack = 1;
archer.stat.defense = 1;
archer.stat.hability = 4;
archer.stat.speed = 3;
archer.price = new Resources();
archer.price.wood = 15;
archer.price.stone = 0;
archer.price.metal = 5;
archer.price.gold = 50;
archer.price.time = 5;
archer.price.points = 29;
archer.quantity = 0;

var knight = new troop();
knight.name = "knight";
knight.stat = new stat();
knight.stat.life = 15;
knight.stat.attack = 4;
knight.stat.defense = 3;
knight.stat.hability = 2;
knight.stat.speed = 3;
knight.price = new Resources();
knight.price.wood = 0;
knight.price.stone = 0;
knight.price.metal = 75;
knight.price.gold = 75;
knight.price.time = 8;
knight.price.points = 38;
knight.quantity = 0;

var militian = new troop();
militian.name = "militian";
militian.stat = new stat();
militian.stat.life = 9;
militian.stat.attack = 2;
militian.stat.defense = 1;
militian.stat.hability = 4;
militian.stat.speed = 2;
militian.price = new Resources();
militian.price.wood = 10;
militian.price.stone = 0;
militian.price.metal = 30;
militian.price.gold = 20;
militian.price.time = 2;
militian.price.points = 28;
militian.quantity = 0;

var knife_thrower = new troop();
knife_thrower.name = "knife_thrower";
knife_thrower.stat = new stat();
knife_thrower.stat.life = 9;
knife_thrower.stat.attack = 2;
knife_thrower.stat.defense = 1;
knife_thrower.stat.hability = 5;
knife_thrower.stat.speed = 4;
knife_thrower.price = new Resources();
knife_thrower.price.wood = 10;
knife_thrower.price.stone = 0;
knife_thrower.price.metal = 30;
knife_thrower.price.gold = 25;
knife_thrower.price.time = 4;
knife_thrower.price.points = 33;
knife_thrower.quantity = 0;

var ranger = new troop();
ranger.name = "ranger";
ranger.stat = new stat();
ranger.stat.life = 12;
ranger.stat.attack = 3;
ranger.stat.defense = 2;
ranger.stat.hability = 4;
ranger.stat.speed = 4;
ranger.price = new Resources();
ranger.price.wood = 20;
ranger.price.stone = 0;
ranger.price.metal = 10;
ranger.price.gold = 30;
ranger.price.time = 6;
ranger.price.points = 43;
ranger.quantity = 0;

/* buildings */
var loggingCamp = new building();
loggingCamp.name = "loggingCamp";
loggingCamp.price = new Resources();
loggingCamp.price.wood = 50;
loggingCamp.price.stone = 50;
loggingCamp.price.metal = 10;
loggingCamp.price.gold = 20;
loggingCamp.price.time = 5;
loggingCamp.quantity = 0;

var stonePit = new building();
stonePit.name = "stonePit";
stonePit.price = new Resources();
stonePit.price.wood = 65;
stonePit.price.stone = 65;
stonePit.price.metal = 20;
stonePit.price.gold = 35;
stonePit.price.time = 7;

var farm = new building();
farm.name = "farm";
farm.price = new Resources();
farm.price.wood = 60;
farm.price.stone = 60;
farm.price.metal = 10;
farm.price.gold = 20;
farm.price.time = 6;

var market = new building();
market.name = "market";
market.price = new Resources();
market.price.wood = 100;
market.price.stone = 100;
market.price.metal = 20;
market.price.gold = 25;
market.price.time = 6;

var barracks = new building();
barracks.name = "barracks";
barracks.price = new Resources();
barracks.price.wood = 120;
barracks.price.stone = 120;
barracks.price.metal = 70;
barracks.price.gold = 55;
barracks.price.time = 12;

var warehouse = new building();
warehouse.name = "warehouse";
warehouse.price = new Resources();
warehouse.price.wood = 80;
warehouse.price.stone = 80;
warehouse.price.metal = 10;
warehouse.price.gold = 15;
warehouse.price.time = 5;

var taxOffice = new building();
taxOffice.name = "taxOffice";
taxOffice.price = new Resources();
taxOffice.price.wood = 150;
taxOffice.price.stone = 150;
taxOffice.price.metal = 20;
taxOffice.price.gold = 40;
taxOffice.price.time = 15;

var ArcheryRange = new building();
ArcheryRange.name = "ArcheryRange";
ArcheryRange.price = new Resources();
ArcheryRange.price.wood = 20;
ArcheryRange.price.stone = 20;
ArcheryRange.price.metal = 50;
ArcheryRange.price.gold = 70;
ArcheryRange.price.time = 8;

var pantheon = new building();
pantheon.name = "pantheon";
pantheon.price = new Resources();
pantheon.price.wood = 200;
pantheon.price.stone = 200;
pantheon.price.metal = 30;
pantheon.price.gold = 100;
pantheon.price.time = 20;

var hunterCamp = new building();
hunterCamp.name = "hunterCamp";
hunterCamp.price = new Resources();
hunterCamp.price.wood = 40;
hunterCamp.price.stone = 40;
hunterCamp.price.metal = 5;
hunterCamp.price.gold = 15;
hunterCamp.price.time = 4;

var show = new building();
show.name = "show";
show.price = new Resources();
show.price.wood = 120;
show.price.stone = 120;
show.price.metal = 30;
show.price.gold = 60;
show.price.time = 10;

var druidHarbor = new building();
druidHarbor.name = "druidHarbor";
druidHarbor.price = new Resources();
druidHarbor.price.wood = 80;
druidHarbor.price.stone = 80;
druidHarbor.price.metal = 40;
druidHarbor.price.gold = 20;
druidHarbor.price.time = 8;

// user initialisation
var user = new user();
user.resources = new Resources();
user.resources.wood = 300;
user.resources.gold = 300;
user.resources.metal = 300;
user.resources.stone = 300;
user.troopList = new Array();
user.buildingList = [];
user.troopList = [];
user.troopList[0] = footman;
user.troopList[0].quantity = 0;
user.troopList[1] = archer;
user.troopList[1].quantity = 0;
user.troopList[2] = knight;
user.troopList[2].quantity = 0;

// TODO initialise buildings with webservice request
loggingCamp.quantity = 3;
user.buildingList[0] = loggingCamp;
stonePit.quantity = 3;
user.buildingList[1] = stonePit;
farm.quantity = 3;
user.buildingList[2] = farm;
market.quantity = 3;
user.buildingList[3] = market;
warehouse.quantity = 3;
user.buildingList[4] = warehouse;
taxOffice.quantity = 3;
user.buildingList[5] = taxOffice;
ArcheryRange.quantity = 3;
user.buildingList[6] = ArcheryRange;
pantheon.quantity = 3;
user.buildingList[7] = pantheon;
hunterCamp.quantity = 3;
user.buildingList[8] = hunterCamp;
show.quantity = 3;
user.buildingList[9] = show;
druidHarbor.quantity = 3;
user.buildingList[10] = druidHarbor;

$(document).ready(function(){
	if ($.cookie('login') == null && $.cookie('password') == null)
	{
		$('#main').load('Home/Info.html');
		$('.offline').show();
		$('.online').hide();
	}
	else
	{
		getUserInfo();
		if ($.cookie('loadedPage') != null)
			$('#main').load($.cookie('loadedPage'));
		else
			$('#main').load('Home/Info.html');
	}
	
	function getUserInfo(){
		$.ajax({
			url : 'http://localhost:8042/login?Creds=' + $.cookie('login') + ';' + $.cookie('password'),
			success : function(data){
				var table = data.split(';');
				if (table[0] == '.')
				{
					user.id = table[1];
					loadTroops();
					loadResources();
					$('.online').show();
					$('.offline').hide();
					$('#connectionForm').css('visibility', 'hidden');
					$('#registrationForm').css('visibility', 'hidden');
				}
				else
				{
					$.cookie('login', null);
					$.cookie('password', null);
					alert('error: ' + table[1]);
				}
				}
		});
	}
	
	function createUser(mail, login, pass, race){
		$.ajax({
					url : 'http://localhost:8042/create?mail='+mail+'&login='+login+'&pass='+pass+'&race='+race,
					success : function(data){alert(data);},
					error: function(){alert('error: problem connecting with webservice');}
			});
	}
	
	function loadTroops(){
		$.getJSON('http://localhost:8084/info?token='+user.id, function(data){
			$.each(data, function(key, val)
			{
				if (key == 'Race')
				{
					user.race = val;
				}
			});
			if (user.race == 'heirs')
			{
				user.troopList[0] = footman;
				user.troopList[1] = archer;
				user.troopList[2] = knight;
			}
			else if (user.race == 'renegade')
			{
				user.troopList[0] = militian;
				user.troopList[1] = knife_thrower;
				user.troopList[2] = ranger;
			}
			for (var i = 0; i < 3; i++)
			{
				var quantity = 0;
				if (user.troopList[i] != null)
				{
					$.getJSON('http://localhost:8084/slots/'+user.troopList[i].name+'/count?token='+user.id, function(data){
						quantity = data;
					});
					user.troopList[i].quantity = quantity;
				}
			}
		});
	}

	function loadResources(){
		$.getJSON('http://localhost:8084/' + user.id + '/resource', function(data){
				$.each(data, function(key, val)
				{
					if (key == "Wood")
						user.resources.wood = val;
					else if (key == "Gold")
						user.resources.gold = val;
					else if (key == "Stone")
						user.resources.stone = val;
					else if (key == "Food")
						user.resources.metal = val;
				});
		});
	}

	// Connection/Register menu tabs (display on enter/click, hide on leave)
	$('#menus>div:nth-child(2)>ul>li').click(function(){
		if ($(this).children("a").html() == 'Login')
		{
			if ($('#connectionForm').css('visibility') == 'hidden')
			{
				$('#connectionForm').css('visibility', 'visible');
				$('#connectionForm').css('height', 'auto');
				$('#connectionForm').css('margin-top', '28px');
				$('#connectionForm').css('padding', '10px 0 10px 0');
				$('#registrationForm').css('visibility', 'hidden');
				$('#registrationForm').css('height', '0em');
				$('#registrationForm').css('margin-top', '0');
				$('#registrationForm').css('padding', '0');
			}
			else
			{
				$('#connectionForm').css('visibility', 'hidden');
			}
		}
		else if ($(this).children("a").html() == 'Register')
		{
			if ($('#registrationForm').css('visibility') == 'hidden')
			{
				$('#connectionForm').css('visibility', 'hidden');
				$('#connectionForm').css('height', '0em');
				$('#connectionForm').css('margin-top', '0');
				$('#connectionForm').css('padding', '0');
				$('#registrationForm').css('visibility', 'visible');
				$('#registrationForm').css('height', 'auto');
				$('#registrationForm').css('margin-top', '28px');
				$('#registrationForm').css('padding', '10px 0 10px 0');
			}
			else
			{
				$('#registrationForm').css('visibility', 'hidden');
			}
		}
		else if ($(this).children("a").html() == 'Deconnexion')
		{
			$('.offline').show();
			$('.online').hide();
			$.cookie('loadedPage', 'Home/Info.html', { expires: 1});
			$('#main').load($.cookie('loadedPage'));
			$.cookie('login', null);
			$.cookie('password', null);
			$.cookie('loadedPage', null);
		}
	});
	
	// Action on Connect button or submit for registration (Fill user data and webservice requests)
	$('.button').click(function(){
		if ($(this).html() == 'Connect')
		{
			$.cookie('login', $("#connectionForm input:eq(0)").val(), { expires: 1});
			$.cookie('password', $("#connectionForm input:eq(1)").val(), { expires: 1});
			getUserInfo();
		}
		else if ($(this).children("a").children("div").html() == 'Submit')
		{
			createUser($("#registrationForm input:eq(3)").val(), $("#registrationForm input:eq(0)").val(), $("#registrationForm input:eq(1)").val(), $("#registrationForm select option:selected").html());
		}
	});

	// Load Ajax Pages on tab click
	$('#menus>div:nth-child(1)>ul>li>ul>li').click(function(){
		if ($(this).children("a").html() == 'Info')
		{
			$('#main').load('Home/Info.html');
			$.cookie('loadedPage', 'Home/Info.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Story')
		{
			$('#main').load('Home/Story.html');
			$.cookie('loadedPage', 'Home/Story.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Races')
		{
			$('#main').load('Home/Races.html');
			$.cookie('loadedPage', 'Home/Races.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Screens')
		{
			$('#main').load('Home/Screens.html');
			$.cookie('loadedPage', 'Home/Screens.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Player')
		{
			$('#main').load('Profile/Player.html');
			$.cookie('loadedPage', 'Profile/Player.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'User')
		{
			$('#main').load('Profile/User.html');
			$.cookie('loadedPage', 'Profile/User.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Troops')
		{
			$('#main').load('Management/Troops.html');
			$.cookie('loadedPage', 'Management/Troops.html', { expires: 1});
		}
		else if ($(this).children("a").html() == 'Buildings')
		{
			$('#main').load('Management/Buildings.html');
			$.cookie('loadedPage', 'Management/Buildings.html', { expires: 1});
		}
	});
});
